package com.service.impl;

import com.dao.CourseMapper;
import com.dao.UserMapper;
import com.pojo.Course;
import com.pojo.User;
import com.service.CourseService;
import com.util.CourseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    final private CourseMapper courseMapper;
    final private CourseUtil courseUtil;
    final private UserMapper userMapper;

    @Autowired(required = false)
    public CourseServiceImpl(CourseMapper courseMapper, CourseUtil courseUtil, UserMapper userMapper){
        this.courseMapper = courseMapper;
        this.courseUtil = courseUtil;
        this.userMapper = userMapper;
    }

    @Override
    public List<Course> findCourseById(String id) {
        List<Course> list;
        try {
            list = courseMapper.selectCourseByID(id);
        } catch (Exception e){
            return null;
        }
        return list;
    }

    @Override
    public String addCourse(String creatorId, String courseName, String className) {
        Course course = new Course();

        course.setCreatorId(creatorId);
        course.setCourseName(courseName);
        course.setClassName(className);

        String id = String.valueOf(courseMapper.getCount() + 1);
        course.setId(id);
        course.setIfShow("yes");
        course.setCourseCode(courseUtil.generateCourseCode(id));

        try {
            courseMapper.insertCourse(course);
        } catch (Exception e){
            e.printStackTrace();
            return "add failed";
        }

        joinCourseByCode(course.getCourseCode(), creatorId);

        return "ok";
    }

    @Override
    public String joinCourseByCode(String courseCode, String studentId) {
        Course course;
        try {
            course = courseMapper.selectCourseByCode(courseCode).get(0);
        } catch (Exception e){
            return "can not find course by code";
        }

        List<String> studentList;
        try {
            studentList = courseUtil.string2List(course.getStudentId());
        } catch (Exception e){
            studentList = new ArrayList<>();
        }

        boolean flag = false;
        for (String id: studentList){
            if (id.equals(studentId)) {
                flag = true;
                break;
            }
        }

        if (flag){
            return "you have joined the course";
        }

        // 在课程表中添加学生的信息
        studentList.add(studentId);
        course.setStudentId(courseUtil.list2String(studentList));

        // 在学生的表中添加课程的信息，无需再次验证是否加入重复的课程
        User user = userMapper.selectUserById(studentId).get(0);
        List<String> courseList;
        try {
            courseList = courseUtil.string2List(user.getCourse());
        } catch (Exception e){
            courseList = new ArrayList<>();
        }
        courseList.add(course.getId());
        user.setCourse(courseUtil.list2String(courseList));
        try {
            userMapper.updateUser(user);
        } catch (Exception e){
            return "failed";
        }


        try {
            courseMapper.updateCourseById(course);
        } catch (Exception e){
            e.printStackTrace();
            return "failed";
        }

        return "ok";
    }

    @Override
    public String dropOutCourse(String studentId, String courseId) {
        Course course;
        User user;
        List<String> studentList;
        List<String> courseList;

        try {
            user = userMapper.selectUserById(studentId).get(0);
        } catch (Exception e){
            return "can not find such student by id";
        }
        try {
            course = courseMapper.selectCourseByID(courseId).get(0);
        } catch (Exception e){
            return "can not find such course by id";
        }


        try {
            studentList = courseUtil.string2List(course.getStudentId());
        } catch (Exception e){
            return "can not find any student in course";
        }


        try {
            courseList = courseUtil.string2List(user.getCourse());
        } catch (Exception e){
            return "the user have not joined any course";
        }

        try {
            studentList.remove(studentId);
        } catch (Exception e){
            return "the user have not joined the course";
        }

        try {
            courseList.remove(courseId);
        } catch (Exception e){
            return "the course does not have such student";
        }

        try {
            course.setStudentId(courseUtil.list2String(studentList));
            user.setCourse(courseUtil.list2String(courseList));
            courseMapper.updateCourseById(course);
            userMapper.updateUser(user);
        } catch (Exception e){
            return "failed";
        }

        return "ok";
    }
}
